#include "CanAction.h"
#include "CanRoute.h"
#include <QDebug>

CanAction::CanAction(const std::string& sigName, const int& value)
    : IplaybackAction(RecoderType::CAN, sigName, value)
{

}

void CanAction::run()
{
    CanRecoderData& data = std::get<CanRecoderData>(itsData);
    int msgId = CanRoute::getInstance()->getMsgIdBySigName(data.canName_.c_str());
    if(msgId != -1)
    {
        CanRoute::getInstance()->sendSignal(msgId, data.canName_.c_str(), data.canValue_);
        qDebug() << "Send : " << QString::number(msgId, 16).toUpper()  << " " << data.canName_  <<" SUCCESS";
    }
    else
    {
        qDebug() << QString::number(msgId, 16).toUpper()  << " " << data.canName_  <<" not find";
    }
}

void CanAction::serializeJson(nlohmann::json& out)
{
    CanRecoderData& data = std::get<CanRecoderData>(itsData);
    out["type"] = 0;
    out["sigName"] = data.canName_.c_str();
    out["sigValue"] = data.canValue_;
}
